using CGF;
using LitJson;
using UnityEngine;

public class TabInvitesCtrl : MonoBehaviour
{
    private TabInvites TabInvites;
    private string inviteCode;

    private void Awake()
    {
        TabInvites = new TabInvites();
        TabInvites.BindProperties(transform);
        
        TabInvites.InviteCode.text = "";
        TabInvites.InviteDesc.text = "";

        TabInvites.BtnCopyCode.SetClick(() =>
        {
            CopyAndPaste.Copy(inviteCode);
            Tip.Open(CommAssets.instance.I18N_copy_success);
        });

        TabInvites.BtnCopyLink.SetClick(() =>
        {
            CopyAndPaste.Copy(TabInvites.InviteDesc.text);
            Tip.Open(CommAssets.instance.I18N_copy_success);
        });

        TabInvites.BtnRule.SetClick(() =>
        {
            PanelMyShareRule.Open();
        });
        
        TabInvites.BtnTG.SetClick(() =>
        {
            CopyAndOpenApp("tg://");
        });
        
        TabInvites.BtnTW.SetClick(() =>
        {
            CopyAndOpenApp("twitter://");
        });
        
        TabInvites.BtnFB.SetClick(() =>
        {
            CopyAndOpenApp("fb://");
        });
        
        /*TabInvites.BtnIG.SetClick(() =>
        {
            CopyAndOpenApp("instagram://");
        });*/
        
        /*TabInvites.BtnWA.SetClick(() =>
        {
            CopyAndOpenApp("whatsapp://");
        });*/
    }

    void OnEnable()
    {
        TabInvites.Ad.sprite = TabInvites.Ad.gameObject.Get<Sprite>((int)CGF.I18N.CurrLanguage);
        ReqData();
    }

    private void CopyAndOpenApp(string scheme)
    {
        CopyAndPaste.Copy(TabInvites.InviteDesc.text);
        Dialog.Alert(TabInvites.I18N_content_is_copied, () =>
        {
            Application.OpenURL(scheme);
        });
    }

    private async void ReqData()
    {
        var res = await Network.Get("/v1/invite");
        JsonData data = res.jsonData["data"];
        inviteCode = data["code"].ToString();
        TabInvites.InviteCode.text = $"{TabInvites.I18N_my_invite_code}: {inviteCode}";
        TabInvites.InviteDesc.text = $"{TabInvites.I18N_share_desc}\n{Config.Get.ShareLink.Replace("{code}", inviteCode)}";
        JsonData levelData = data["level_count"];
        int totalCount = 0;
        if (levelData != null)
        {
            for (int i = 0; i < levelData.Count; i++)
            {
                totalCount += (int)levelData[i]["count"];
            }
        }
        TabInvites.MyInviteCount.text = $"{TabInvites.I18N_my_invite_count}: {totalCount}";
        Vector2 size = TabInvites.InviteDesc.rectTransform.sizeDelta;
        size.y = TextHelper.GetTextHeight(TabInvites.InviteDesc);
        TabInvites.InviteDesc.rectTransform.sizeDelta = size;
        int[] counts = new int[5];
        if (levelData != null)
        {
            for (int i = 0; i < levelData.Count; i++)
            {
                int level = (int)levelData[i]["level"];
                counts[level - 1] = (int)levelData[i]["count"];
            }
        }
        TabInvites.CountsText1.text = counts[0].ToString();
        TabInvites.CountsText2.text = counts[1].ToString();
        TabInvites.CountsText3.text = counts[2].ToString();
        TabInvites.CountsText4.text = counts[3].ToString();
        TabInvites.CountsText5.text = counts[4].ToString();
    }
}
